# 设置输出编码为UTF-8
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8

# 获取当前目录及一级子目录
$directories = @(Get-Item -Path .) + @(Get-ChildItem -Directory -Path .)
# 创建哈希表存储哈希值和对应的文件名
$hashTable = @{}
$num2 = 0
foreach ($dir in $directories) {
    Write-Output "`n===== handling the Dir: $($dir.FullName) ====="
    
    # 获取当前目录下所有扩展名为.safetensors的文件
    $files = Get-ChildItem -File -Path $dir.FullName -Filter "*.safetensors"
    
    if ($files.Count -eq 0) {
        #Write-Output "not found andy .safetensorsfile in this Dir。"
        continue
    }
	# 创建哈希表存储哈希值和对应的文件名
	
	$num = 0
	
	# 计算每个文件的哈希值并存储到哈希表中
	foreach ($file in $files) {
		try {
			$hash = (Get-FileHash -Path $file.FullName -Algorithm SHA256).Hash
			$num++
			# 输出当前文件的哈希值
			Write-Output "Done $num : $($file.Name) - $hash"
			
			if ($hashTable.ContainsKey($hash)) {
				$hashTable[$hash] += ", $($file.Name)"
			} else {
				$hashTable.Add($hash, $file.Name)
			}
		} catch {
			Write-Error "error while handling file: '$($file.Name)' Error: $_"
		}
	}
	
	$num2+=$num
}
Write-Output "`n===== handling the total $num2 files ====="
# 筛选出有重复哈希值的文件
$duplicates = $hashTable.GetEnumerator() | Where-Object { $_.Value -match "," }

# 输出结果
if ($duplicates.Count -gt 0) {
	Write-Output "Same hash value Found: "
	Write-Output "------------------------"
	foreach ($duplicate in $duplicates) {
		Write-Output "hash: $($duplicate.Name)"
		Write-Output "fileName: $($duplicate.Value)"
		Write-Output "------------------------"
	}
} else {
	Write-Output "no duplicate file found."
}

Write-Output "total $num files is computed."




    